package com.sky.myapplication.kotlin.kotlinActivity

import android.Manifest
import android.content.ContentValues
import android.content.pm.PackageManager
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.sky.myapplication.R
import com.sky.myapplication.kotlin.helper.DBHelper
import java.io.*


/**
 * @Author: Minxp
 * @UpdateDate: 2021/7/8 0008
 * @Description: java类作用描述
 */
class SQLiteKotlinActivity: AppCompatActivity() {
    lateinit var et_input: EditText
    lateinit var et_id: EditText
    lateinit var tv_text: TextView
    lateinit var dbHelper: DBHelper
    lateinit var db: SQLiteDatabase

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sqlite)

        dbHelper = DBHelper(this, "sky.db", 1);
        db = dbHelper.writableDatabase

        initView()
        showData()
    }

    fun initView() {
        var btn_add_sqlite: Button = findViewById(R.id.btn_add_sqlite)
        var btn_update_sqlite: Button = findViewById(R.id.btn_update_sqlite)
        var btn_delete_sqlite: Button = findViewById(R.id.btn_delete_sqlite)
        var btn_show_sqlite: Button = findViewById(R.id.btn_show_sqlite)

        et_input = findViewById(R.id.et_input)
        et_id = findViewById(R.id.et_id)
        tv_text = findViewById(R.id.tv_text)

        btn_add_sqlite.setOnClickListener {
            addData()
        }

        btn_update_sqlite.setOnClickListener {
            updateData()
        }

        btn_delete_sqlite.setOnClickListener {
            deleteData()
        }

        btn_show_sqlite.setOnClickListener {
            showData()
        }
    }

    private fun addData() {
        if (et_input.text.toString().equals("")) return

        var values = ContentValues()
        values.put("name", et_input.text.toString())
        db.insert("sky", null, values)

        showData()
    }

    private fun updateData() {
        var value = ContentValues()
        value.put("name", et_input.text.toString())
        var where = "id="+et_id.text.toString().trim()
        if (db.update("sky", value, where, null) > 0) {
            Toast.makeText(this, "数据更新成功！", Toast.LENGTH_SHORT).show()
            showData()
        } else {
            Toast.makeText(this, "数据未更新", Toast.LENGTH_SHORT).show()
        }
    }

    private fun deleteData(){
        var where = "id="+et_id.text.toString().trim()
        if(db.delete("sky", where, null) > 0) {
            Toast.makeText(this, "数据删除成功！", Toast.LENGTH_SHORT).show()
            showData()
        } else {
            Toast.makeText(this, "数据未删除", Toast.LENGTH_SHORT).show()
        }
    }

    private fun showData(){
        var showText = ""
        var cursor = db.query("sky", null, null, null, null, null, "id ASC")
        cursor.moveToFirst()
        while (!cursor.isAfterLast){
            showText += "id: "+cursor.getString(0)+"  name: "+cursor.getString(1)+"\n"
            cursor.moveToNext()
        }
        tv_text.text = showText
    }
}